package com.jzqf.harmonydemo.views.slice;

import com.jzqf.harmonydemo.ResourceTable;
import com.jzqf.harmonydemo.utils.LogUtil;
import com.jzqf.harmonydemo.utils.ToastUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.utils.Color;

/**
 * 基础组件
 *
 * @author LiuWeiHao
 * 2021/5/6 10:58
 */
public class ComponentsAbilitySlice extends AbilitySlice {
    private final String TAG = ComponentsAbilitySlice.class.getSimpleName();
    private Image mImage;
    private TextField mTextField;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_components);
        initView();
        initSwitch();
        initProgressBar();
        initEvents();
        LogUtil.d(TAG, "onStart");
    }

    private void initProgressBar() {
        ProgressBar progressBar = (ProgressBar) findComponentById(ResourceTable.Id_components_progressBar);
        progressBar.setDividerLineColor(Color.BLUE);
        progressBar.setProgressValue(50);
        progressBar.setProgressHintText("50%");
    }

    private void initEvents() {
        mTextField.addTextObserver(new Text.TextObserver() {
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                LogUtil.d(TAG, s);
            }
        });
    }

    private void initView() {
        mImage = (Image) findComponentById(ResourceTable.Id_components_image);
//        mImage.setPixelMap(ResourceTable.Media_icon_cup);
        mTextField = (TextField) findComponentById(ResourceTable.Id_components_textField);
    }

    private void initSwitch() {
        Switch switchBtn = (Switch) findComponentById(ResourceTable.Id_components_switch);
        //开启状态下滑块的样式
        ShapeElement elementThumbOn = new ShapeElement();
        elementThumbOn.setShape(ShapeElement.OVAL);
        elementThumbOn.setRgbColor(RgbColor.fromArgbInt(0xFF11D42B));
        elementThumbOn.setCornerRadius(50);
        // 关闭状态下滑块的样式
        ShapeElement elementThumbOff = new ShapeElement();
        elementThumbOff.setShape(ShapeElement.OVAL);
        elementThumbOff.setRgbColor(RgbColor.fromArgbInt(0xFFFFFFFF));
        elementThumbOff.setCornerRadius(50);
        // 开启状态下轨迹样式
        ShapeElement elementTrackOn = new ShapeElement();
        elementTrackOn.setShape(ShapeElement.RECTANGLE);
        elementTrackOn.setRgbColor(RgbColor.fromArgbInt(0x7311D42B));
        elementTrackOn.setCornerRadius(50);
        // 关闭状态下轨迹样式
        ShapeElement elementTrackOff = new ShapeElement();
        elementTrackOff.setShape(ShapeElement.RECTANGLE);
        elementTrackOff.setRgbColor(RgbColor.fromArgbInt(0xFF808080));
        elementTrackOff.setCornerRadius(50);
        switchBtn.setTrackElement(trackElementInit(elementTrackOn, elementTrackOff));
        switchBtn.setThumbElement(thumbElementInit(elementThumbOn, elementThumbOff));
        switchBtn.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean isChecked) {
                ToastUtil.show(getContext(), "isChecked=" + isChecked);
            }
        });
    }

    private StateElement trackElementInit(ShapeElement on, ShapeElement off) {
        StateElement trackElement = new StateElement();
        trackElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, on);
        trackElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, off);
        return trackElement;
    }

    private StateElement thumbElementInit(ShapeElement on, ShapeElement off) {
        StateElement thumbElement = new StateElement();
        thumbElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, on);
        thumbElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, off);
        return thumbElement;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
